#include "cboard.h"

CBoard::CBoard()
{
	_clear();
}

CBoard::~CBoard()
{
}
 
int CBoard::_clear()
{
	for( int i =  0; i < 128; i++ ) board[ i ] = OUT;
	for( int k = 0; k < 8; k++  ) 
		for( int i = 0; i < 8 ; i++ ) 
			board[ cord_to_square(i,k) ] = 0;
	for( int i = 0; i < 64; i++ )
		piece[ i ] = OUT;	
	return 0;
}

int CBoard::init()
{
	_clear();
	return 0;
}
int CBoard::_printPiece( uint pie )
{
			if ( pie == 0 )
				cout << ".";
			else if ( IS_WHITE(pie)) 
			{
				switch( GET_PIECE_TYPE(pie) )	
				{
					case PAWN 	: cout << "P"; break;
					case KNIGHT : cout << "N"; break;
					case BISHOP : cout << "B"; break;
					case ROOK 	: cout << "R"; break;
					case QUEEN 	: cout << "Q"; break;
					case KING 	: cout << "K"; break;
					default : cout << "?"; break;
				}			
			}
			else if ( IS_BLACK(pie) )
			{
				switch( GET_PIECE_TYPE(pie) )	
				{
					case PAWN 	: cout << "p"; break;
					case KNIGHT : cout << "n"; break;
					case BISHOP : cout << "b"; break;
					case ROOK 	: cout << "r"; break;
					case QUEEN 	: cout << "q"; break;
					case KING 	: cout << "k"; break;
					default : cout << "?"; break;
				}				
			}
	return 0;
}
int CBoard::print( )
{
	for( int k = 7; k >= 0; k--  ) 
	{
		for( int i = 0; i < 8 ; i++ ) 
		{
			int sq = cord_to_square(i,k);
			uint pie = board[sq];
			_printPiece( pie );
		}
		cout << endl;
	}
	return 0;
}

int CBoard::setPiece( uint side, uint pie, uint sq )
{
	for( int id = 0; id < 16; id++ )
	{
		uint p = piece[side|id];
		cout << p << endl;
		if (IS_OUT( p ) )
		{
			piece[ side|id ] 	= sq;
			board[ sq ] 		= side | id | ( pie<<6 );
			return 0;
		}
	}
	return -1;
}

int CBoard::_saveSlideMove( CList * list, uint side, uint move_from, uint move_to )
{
	T_MOVE m;
	
	m = (T_MOVE)move_from | (((T_MOVE)move_to) << 8) | (((T_MOVE)board[move_to]) << 16);
	
	
	return 0;
}

int CBoard::_genSlideMoves( CList * list, uint side, uint move_from, uint move_type, uint move_distance )
{
	if ( move_type & MOVE_TYPE_KNIGHT )
	{
		// knight moves
		uint move_to;
		move_to = move_from + 8 ; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from + 19; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from + 21; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from + 12; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from - 8 ; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from - 19; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from - 21; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		move_to = move_from - 12; if ( MOVE_OK( board[move_to], side ) ) _saveSlideMove( list, side, move_from, move_to );
		
	}
	if ( move_type & MOVE_TYPE_BISHOP )
	{
	}
	if ( move_type & MOVE_TYPE_ROOK )
	{
	}
	return 0;
}

int CBoard::genAllMoves( CList * list, uint side )
{
	for( int id = 0; id < 16; id++ )
	{
		uint p = piece[side|id];
		if (!IS_OUT( p ) )
		{
			uint pie = board[ p ];
			uint piece_type = GET_PIECE_TYPE(pie);
			switch(piece_type)
			{
				case KNIGHT:_genSlideMoves( list, side, p, MOVE_TYPE_KNIGHT, 1 ); break;
				case BISHOP:_genSlideMoves( list, side, p, MOVE_TYPE_BISHOP, 10 ); break;
				case ROOK: 	_genSlideMoves( list, side, p, MOVE_TYPE_ROOK, 10 ); break;
				case QUEEN:	_genSlideMoves( list, side, p, MOVE_TYPE_BISHOP|MOVE_TYPE_ROOK, 10 ); break;
				case KING: 	_genSlideMoves( list, side, p, MOVE_TYPE_BISHOP|MOVE_TYPE_ROOK, 1 ); break;	
			}	
			return 0;
		}
	}
	return 0;
}
